
clear all
global codepath "/Users/xiangfang/Dropbox/Project/Sovereign Bond Project/Replication_package_submission_RFS/Code"
global datapath "/Users/xiangfang/Dropbox/Project/Sovereign Bond Project/Replication_package_submission_RFS/Data/Paper_Data"
global outTEX "/Users/xiangfang/Dropbox/Project/Sovereign Bond Project/Replication_Package_submission_RFS/Tables"
cd "$codepath"
include "${codepath}/iv_construction_ae.do"

use "${datapath}/holding_ae.dta", clear

// Manually drop sample
drop if country == "Greece" & year>2010 & year<2014  // > 2010 and <2014
drop if yield_5y<0 // drop negative yield obs

******************* Instrument Construction *****************************
// Preparations
gen l_total_usd = ln(Total)
by country: gen lag_total = l_total_usd[_n-1]
by country: gen lag_ltot_gdp = l_tot_gdp[_n-1]
by country: gen lag_tot_gdp = Total[_n-1]/gdp[_n] // What we want is (D_t-a*D_t-1)/GDP_t depends on characteristics

generate_iv_ols_supply, instrument_var(ols_supply) xvars(real_gdp_growth inflation l_exp_ratio rate_num_loc) fixedeffects(i.country_id i.year) yr(5)
// Winsorization
winsor yield_5y, gen(yield_5y1) p(0.01)
winsor ols_supply_yield, gen(ols_supply_yield1) p(0.01)
// 1. Benchmark results:
gen l_yield_5y1 = ln(yield_5y1)
gen l_ols_supply_yield1 = ln(ols_supply_yield1)
label variable yield_5y "yield_5y"

eststo clear
foreach x in DomBK DomNB DomCB ForBK ForNB ForCB{
	eststo: ivreghdfe l_`x'_mkt l_gdp real_gdp_growth inflation l_exp_ratio rate_num_loc (l_yield_5y = ols_supply_yield1), absorb (country_id year) noconstant
}
esttab est1 est2 est3 est4 est5 est6 using "${outTEX}/Table6.tex", replace b(%5.3f) se(%5.3f) label booktabs star(* .1 ** .05 *** .01)  nogaps noomit nogaps nobase ///
order(l_yield_5y real_gdp_growth l_gdp l_exp_ratio rate_num_loc) ///
	title("Yield Elasticity: Instrumental Variable Estimate") ///
	mtitles( "\shortstack{DomBK}" "\shortstack{DomNB}" "\shortstack{DomCB}" "\shortstack{ForBK}" "\shortstack{ForNB}" "\shortstack{ForCB}" "\shortstack{Total}" ) nonotes

